﻿using Prism.Commands;
using Prism.Mvvm;
using ProcessControl.Model;
using System;
using System.Windows.Input;

namespace ProcessControl.ViewModels
{
    public class YieldViewModel : BindableBase
    {
        RwData RWData;

        public YieldViewModel()
        {
            string dataPath = string.Format(@"{0}\Config\{1}\{2}.xml", Environment.CurrentDirectory, "Total", "total");
            RWData = new RwData(dataPath);
            RWData.Ini();
            Total = RWData.Total;
            NG = RWData.NG;
        }

        private ulong total;
        public ulong Total
        {
            get { return total; }
            set
            {
                SetProperty(ref total, value, () =>
                {
                    RWData.Total = value;
                    CalculateYield();
                });
            }
        }

        private ulong ng;
        public ulong NG
        {
            get { return ng; }
            set
            {
                SetProperty(ref ng, value, () =>
                {
                    RWData.NG = value;
                    CalculateYield();
                });
            }
        }

        public double Yield { get; set; }

        private void CalculateYield()
        {
            if (Total > 0)
                Yield = Math.Round(100.000 - (NG * 100.000) / Total, 3);
            else
                Yield = 100;
        }


        public ICommand ClearCommand
        { get => new DelegateCommand(Clear); }

        private void Clear()
        {
            NG = 0;
            Total = 0;
        }
    }
}
